﻿<template>
  <a-modal
    title="新增数据库"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入标题" v-decorator="['title', {rules: [{required: true, message: '请输入标题！'}]}]" />
        </a-form-item>
        <a-form-item label="数据库用户名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库用户名" v-decorator="['userName', {rules: [{required: true, message: '请输入数据库用户名！'}]}]" />
        </a-form-item>
        <a-form-item label="数据库用户密码" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库用户密码" v-decorator="['password', {rules: [{required: true, message: '请输入数据库用户密码！'}]}]" />
        </a-form-item>
        <a-form-item label="数据库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库名称" v-decorator="['dbName', {rules: [{required: true, message: '请输入数据库名称！'}]}]" />
        </a-form-item>
        <a-form-item label="安全级别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择安全级别" v-decorator="['securityClaim', {rules: [{ required: true, message: '请选择安全级别！' }]}]">
            <a-select-option v-for="(item,index) in securityClaimData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="数据库连接的其他选项，JSON字符串存储" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库连接的其他选项，JSON字符串存储" v-decorator="['options']" />
        </a-form-item>
        <a-form-item label="该数据库的负责人ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入该数据库的负责人ID" v-decorator="['managerId']" />
        </a-form-item>
        <a-form-item label="所属应用ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入所属应用ID" v-decorator="['applicationId']" />
        </a-form-item>
        <a-form-item label="所属应用名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入所属应用名称" v-decorator="['applicationName']" />
        </a-form-item>
        <a-form-item label="数据库服务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库服务ID" v-decorator="['dbServerId']" />
        </a-form-item>
        <a-form-item label="备份文件存放路径" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入备份文件存放路径" v-decorator="['localBackupDir']" />
        </a-form-item>
        <a-form-item label="远程存储ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入远程存储ID" v-decorator="['remoteStoreId']" />
        </a-form-item>
        <a-form-item label="远程备份路径" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入远程备份路径" v-decorator="['remoteBackupDir']" />
        </a-form-item>
        <a-form-item label="备份保留天数" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入备份保留天数" style="width: 100%" v-decorator="['backupSaveDays']" />
        </a-form-item>
        <a-form-item label="月末备份保留个数" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入月末备份保留个数" style="width: 100%" v-decorator="['monthlyBackupSaveCount']" />
        </a-form-item>
        <a-form-item label="数据库测试脚本" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入数据库测试脚本" v-decorator="['testSql']" />
        </a-form-item>
        <a-form-item label="清理日志时保留日志文件大小：如果操作类型为清理日志时选填，单位MB" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入清理日志时保留日志文件大小：如果操作类型为清理日志时选填，单位MB" v-decorator="['minLogSize']" />
        </a-form-item>
        <a-form-item label="如何操作" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入如何操作" v-decorator="['backupFilenameFormat']" />
        </a-form-item>
        <a-form-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['isEnabled', { valuePropName: 'checked' }]" />
        </a-form-item>
        <a-form-item label="逻辑数据文件名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入逻辑数据文件名" v-decorator="['logicDataName']" />
        </a-form-item>
        <a-form-item label="逻辑日志文件名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入逻辑日志文件名" v-decorator="['logicLogName']" />
        </a-form-item>
        <a-form-item label="默认测试数据库服务ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入默认测试数据库服务ID" v-decorator="['testDbServerId']" />
        </a-form-item>
        <a-form-item label="最大增量备份天数" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入最大增量备份天数" style="width: 100%" v-decorator="['maxIncrementBackupDays']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    DbAdd
  } from '@/api/modular/main/DbManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        securityClaimData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
	// 初始化方法
	add (record) {
	this.visible = true
	const securityClaimOption = this.$options
	this.securityClaimData = securityClaimOption.filters['enumData']('SecurityLevel')
	},
	/**
	* 提交表单
	*/
	handleSubmit () {
	const { form: { validateFields } } = this
	this.confirmLoading = true
	validateFields((errors, values) => {
	if (!errors) {
	for (const key in values) {
	// WARN: 有可能导致类型错误，注意
	if ((values[key]) !== null && typeof (values[key]) === 'object') {
	values[key] = JSON.stringify(values[key])
	}
	}
	DbAdd(values).then((res) => {
	if (res.success) {
	this.$message.success('新增成功')
	this.confirmLoading = false
	this.$emit('ok', values)
	this.handleCancel()
	} else {
	this.$message.error('新增失败：' + JSON.stringify(res.message))
	}
	}).finally((res) => {
	this.confirmLoading = false
	})
	} else {
	this.confirmLoading = false
	}
	})
	},
	handleCancel () {
	this.form.resetFields()
	this.visible = false
	}
	}
	}
</script>
